package daimasuixiangmu.btree;

/**
 * TODO
 *
 * @author gaojiaxing
 * @version 1.0.0
 * @since 2021/05/26 09:27
 */
public class TreeNode {

  private TreeNode left;
  private TreeNode right;
  private int val;

  public TreeNode(TreeNode left, TreeNode right, int val) {
    this.left = left;
    this.right = right;
    this.val = val;
  }

  public TreeNode getLeft() {
    return left;
  }

  public TreeNode getRight() {
    return right;
  }

  public int getVal() {
    return val;
  }

  public TreeNode add(int val){
    // 右边
    if (this.val < val) {
      if (this.right == null){
        this.right = new TreeNode(null,null,val);
        return this.right;
      }
      return this.right.add(val);
    } else if (this.val > val){
      if (this.left == null) {
        this.left = new TreeNode(null,null,val);
        return this.left;
      }
      return this.left.add(val);
    }
    return null;
  }

  public static void main(String[] args) {
    TreeNode root = new TreeNode(null,null,5);
    root.add(1);
    root.add(7);
    root.add(8);
    root.add(6);
    root.add(3);
    root.add(2);

    System.out.println(root);
  }
}

